import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 数据
categories = ["口红", "面膜", "隔离", "防晒", "精华"]
sales_2021 = np.array([3568, 4135, 4436, 4106, 4936])
sales_2022 = np.array([2569, 3241, 2965, 3209, 3541])

# 设置图表背景色
fig, ax = plt.subplots(figsize=(10, 6))
fig.patch.set_facecolor('#1A1E43')
ax.set_facecolor('#1A1E43')
ax.grid(False)

# X轴位置
x = np.arange(len(categories))

# 柱状图宽度
width = 0.2

# 画柱状图
bars1 = ax.bar(x - width / 2, sales_2021, width, label='2021销量', color='#0070C0')
bars2 = ax.bar(x + width / 2, sales_2022, width, label='2022销量', color='#82ADD7')


# 添加文本标签
def autolabel(bars, sales_data):
    for bar, sales in zip(bars, sales_data):
        height = bar.get_height()
        ax.annotate('{}'.format(sales),
                    xy=(bar.get_x() + bar.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom', color='white', fontsize=14)


autolabel(bars1, sales_2021)
autolabel(bars2, sales_2022)

# 设置标题和标签
ax.set_xticks(x)
ax.set_xticklabels(categories, fontsize=12, color='#d3d5e2')
ax.legend(loc='upper left', fontsize=12, facecolor='#212A42', edgecolor='#212A42', labelcolor=['#0070C0', '#83b5e1'])
ax.tick_params(axis='y', colors='#d3d5e2')

# 隐藏 y 轴刻度
ax.set_yticklabels([])
# 隐藏坐标轴，保留刻度
for position in ['left', 'right', 'top']:
    ax.spines[position].set_color('none')
ax.spines['bottom'].set_color('#5D607A')
# 添加副标题
# 添加标题
plt.title('2022年商品对比去年销售情况', fontsize=24, weight='bold', loc='left', pad=20, color='white')
# 添加子标题
plt.text(0.31, 1.025, '商品整体比去年销量有所下降，其中隔离下降最多，下降33%', ha='center', va='center',
         transform=plt.gca().transAxes, fontsize=15, color='white')

# 显示图表
plt.tight_layout(pad=3)
plt.show()
